//
//  ViewController.m
//  ShowProductsDemo
//
//  Created by hibo on 15/12/1.
//  Copyright © 2015年 sunping. All rights reserved.
//

#import "CategoryViewController.h"
#import "LeftViewCell.h"
#import "otherCollView.h"
#import "otherLayout.h"
#import "otherRHeader1.h"
#import "otherRHeader2.h"

//设备宽
#define KDeviceWidth self.view.frame.size.width
//设备高
#define KDeviceHeight self.view.frame.size.height
//背景色
#define  SPColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]

//头视图高
const NSInteger KTopHeight = 40;
//左侧宽
const NSInteger KLeftWidth = 80;

NSInteger _tag = 0;

@interface CategoryViewController ()<LeftViewCellDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic,retain)NSMutableArray* productArray;
@property(nonatomic,retain)UIImageView* scanView;//扫描视图

@property(nonatomic,retain)UIImageView* baseView;

//初始化左侧视图
-(void)initLeftView;

@end

@implementation CategoryViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    _leftViewCellArray = [NSMutableArray array];
    [self initLeftView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//获取左侧视图数据
-(NSArray*)leftDataArray{
    if (_leftDataArray == nil) {
        NSString* path = [[NSBundle mainBundle] pathForResource:@"categorys_name.plist" ofType:nil];
        
        self.leftDataArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _leftDataArray;
}

//加载左侧视图
-(void)initLeftView{
    
    UIScrollView* leftScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, 80, KDeviceHeight-KTopHeight-20)];
    leftScrollView.backgroundColor = SPColor(244, 244, 244);
    leftScrollView.showsVerticalScrollIndicator = NO;
    NSInteger height = 0;
    for (int index = 0; index < self.leftDataArray.count; index++) {
        LeftViewCell* leftView = [LeftViewCell leftViewCell];
        
        leftView.delegate = self;
        leftView.frame = CGRectMake(0, height, KLeftWidth, 44);
        
        [leftView.nameBtn setTitle:_leftDataArray[index] forState:UIControlStateNormal];
        
        leftView.nameBtn.tag = index+101;
        leftView.redImage.tag = index+201;
        
        [leftScrollView addSubview:leftView];
        [_leftViewCellArray addObject:leftView];
        height += 44;
        
        if (index == 0) {
            [self changeChoice:index];
            _tag = index;
        }
    }
    
    leftScrollView.contentSize = CGSizeMake(80, height);
    self.leftScrollView = leftScrollView;
    
    [self.view addSubview:_leftScrollView];
    
}


//改变点击状态
-(void)changeChoice:(NSInteger)tag{
    
    LeftViewCell *leftView = [_leftViewCellArray objectAtIndex:_tag];
    [leftView setSelected:NO tag:_tag];
    
    LeftViewCell *leftView2 = [_leftViewCellArray objectAtIndex:tag];
    [leftView2 setSelected:YES tag:tag];
    
    _tag = tag;
    _choice = tag;
    
    [self loadCates];
}

#pragma mark - 注册不同流式单元格
static NSString *const otherCollID = @"otherCell";//上图下字
static NSString *const otherID1 = @"otherRH1";//图和字和线
static NSString *const otherID2 = @"otherRH2";//字和线

-(void)loadCates{
    
    [self.collectionView reloadData];
    
    CGRect rect = CGRectMake(KLeftWidth+5, 60, KDeviceWidth-10-KLeftWidth, KDeviceHeight-20-KTopHeight);
    _layout = [[otherLayout alloc]init];
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:_layout];
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [collectionView registerNib:[UINib nibWithNibName:@"otherCollView" bundle:nil] forCellWithReuseIdentifier:otherCollID];
    [collectionView registerNib:[UINib nibWithNibName:@"otherRHeader1" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:otherID1];
    [collectionView registerNib:[UINib nibWithNibName:@"otherRHeader2" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:otherID2];
    
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.showsVerticalScrollIndicator = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
}

#pragma mark - collectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    otherCollView* cell = [collectionView dequeueReusableCellWithReuseIdentifier:otherCollID forIndexPath:indexPath];

    return cell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    NSString *reuseIdentifier = @"";
    
    if (_choice == 20){
        UICollectionReusableView* collRV = [[UICollectionReusableView alloc]init];
        collRV.frame = CGRectMake(0, 0, 230, 0);
        return collRV;
    }
    else if (_choice == 2 || _choice == 10){
        reuseIdentifier = @"otherRH2";
        otherRHeader2* collRV = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
        return collRV;
    }
    else{
        if (indexPath.section == 0){
            reuseIdentifier = @"otherRH1";
            otherRHeader1* collRV1 = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
            collRV1.frame = CGRectMake(0, 0, 230, 123);
            return collRV1;
        }else{
            reuseIdentifier = @"otherRH2";
            otherRHeader2* collRV2 = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
            return collRV2;
        }
        
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (_choice == 2 || _choice == 10)
    {
        return CGSizeMake(200, 15);
    }
    else if (_choice == 20)
    {
        return CGSizeMake(200, 0);
    }
    else{
        if (section == 0) {
            return CGSizeMake(200, 123);
        }
        return CGSizeMake(200, 15);
    }
}

@end

